package com.wordLearning.word.application.controller;

import com.google.common.base.Preconditions;
import com.wordLearning.word.application.convert.WordInfoDTOConverter;
import com.wordLearning.word.application.dto.WordDTO;
import com.wordLearning.word.common.entity.PageResult;
import com.wordLearning.word.common.entity.Result;
import com.wordLearning.word.domain.entity.WordBO;
import com.wordLearning.word.domain.service.WordInfodomainService;

import com.wordLearning.word.infra.basic.entity.FavoriteWord;
import com.wordLearning.word.infra.basic.entity.UnmasteredWord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 单词模块
 */
@RestController
@RequestMapping("/word")
@Slf4j
public class WordController {

    @Resource
    private WordInfodomainService wordInfodomainService;


    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody WordDTO wordDTO){
        try{
            System.out.println(wordDTO);
            Preconditions.checkArgument(!StringUtils.isBlank(wordDTO.getDefinition()),"单词解释不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(wordDTO.getExample()),"单词例句不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(wordDTO.getPhonetic()),"单词音标不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(wordDTO.getSpelling()),"单词拼写不能为空！");
            Preconditions.checkArgument(!StringUtils.isBlank(wordDTO.getPartOfSpeech()),"单词属性不能为空！");
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            wordInfodomainService.add(wordBO);

            return Result.ok(true);
        }catch(Exception e){
            log.error("WordController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增单词失败");
        }
    }

    /**
     * 删除单词
     */
    @DeleteMapping("/delete")
    public Result<Boolean> delete(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            Boolean result = wordInfodomainService.delete(wordBO);
            return Result.ok(result);
        } catch(Exception e){
            log.error("WordController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除单词失败");
        }
    }

    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody WordDTO wordDTO){
        try{
            Preconditions.checkNotNull(wordDTO.getWordId(),"单词id不能为空");
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            Boolean result = wordInfodomainService.update(wordBO);
            return Result.ok(result);
        } catch(Exception e){
            log.error("WordController.update.error:{}",e.getMessage(),e);
            return Result.fail("更新单词信息失败");
        }
    }


    @PostMapping("/getWordPage")
    public Result<PageResult<WordDTO>> getWordPage(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            wordBO.setPageNo(wordDTO.getPageNo());
            wordBO.setPageSize(wordDTO.getPageSize());
            PageResult<WordBO> boPageResult = wordInfodomainService.getWordPage(wordBO);
            return Result.ok(boPageResult);
        } catch(Exception e){
            log.error("WordController.getWordPage.error:{}",e.getMessage(),e);
            return Result.fail("分页查询单词失败");
        }
    }

    @PostMapping("/getFavoritePage")
    public Result<PageResult<FavoriteWord>> getFavoritePage(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            wordBO.setPageNo(wordDTO.getPageNo());
            wordBO.setPageSize(wordDTO.getPageSize());
            PageResult<FavoriteWord> pageResult = wordInfodomainService.getFavoritePage(wordBO);
            return Result.ok(pageResult);
        } catch(Exception e){
            log.error("WordController.getFavoritePage.error:{}",e.getMessage(),e);
            return Result.fail("分页查询题目失败");
        }
    }

    @PostMapping("/getUnmasterPage")
    public Result<PageResult<UnmasteredWord>> getUnmasterPage(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            wordBO.setPageNo(wordDTO.getPageNo());
            wordBO.setPageSize(wordDTO.getPageSize());
            PageResult<UnmasteredWord> pageResult = wordInfodomainService.getUnmasterPage(wordBO);
            return Result.ok(pageResult);
        } catch(Exception e){
            log.error("WordController.getFavoritePage.error:{}",e.getMessage(),e);
            return Result.fail("分页查询题目失败");
        }
    }

    @PostMapping("/queryWordBySpelling")
    public Result<WordDTO> queryWordBySpelling(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            WordDTO resDTO = WordInfoDTOConverter.INSTANCE.convertBOToDTO(wordInfodomainService.queryWordBySpelling(wordBO));
            return Result.ok(resDTO);
        } catch (Exception e){
            log.error("WordController.queryWordBySpelling.error:{}",e.getMessage(),e);
            return Result.fail("单词查询失败");
        }
    }

    @PostMapping("/queryWordById")
    public Result<WordDTO> queryWordById(@RequestBody WordDTO wordDTO){
        try{
            WordBO wordBO = WordInfoDTOConverter.INSTANCE.convertDTOToBO(wordDTO);
            WordDTO resDTO = WordInfoDTOConverter.INSTANCE.convertBOToDTO(wordInfodomainService.queryWordById(wordBO));
            return Result.ok(resDTO);
        } catch (Exception e){
            log.error("WordController.queryWordById.error:{}",e.getMessage(),e);
            return Result.fail("单词查询失败");
        }
    }

    @PostMapping("/countLearningRecord")
    public Result<Integer> countLearningRecord(){
        try{
            Integer res = wordInfodomainService.countLearningRecord();
            return Result.ok(res);
        } catch (Exception e){
            log.error("WordController.countLearningRecord.error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    @PostMapping("/countUnmasteredWord")
    public Result<Integer> countUnmasteredWord(){
        try{
            Integer res = wordInfodomainService.countUnmasterWord();
            return Result.ok(res);
        } catch (Exception e){
            log.error("WordController.countUnmasteredWord.error:{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

}
